package com.neusoft.service.impl;

import com.neusoft.entity.DrugInfo;
import com.neusoft.mapper.DrugInfoMapper;
import com.neusoft.service.DrugInfoService;
import com.neusoft.utils.Result;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author akemihomurasama
 */
@Service
public class DrugInfoServiceImpl implements DrugInfoService {
    @Autowired
    private DrugInfoMapper drugInfoMapper;

    /**
     * 根据条件分页查询药品信息
     *
     * @param drugInfo    查询的条件
     * @param pageSize    每页数量
     * @param currentPage 当前页码
     * @return 药品信息列表
     */
    @Override
    public Result queryDrugByPageAndCondition(DrugInfo drugInfo, int currentPage, int pageSize) {
        return Result.ok(drugInfoMapper.queryDrugByPageAndCondition(drugInfo, (currentPage - 1) * pageSize, pageSize));
    }

    /**
     * 查询药品总数量
     *
     * @return 返回数量
     */
    @Override
    public Result queryDrugNum() {
        return Result.ok(drugInfoMapper.queryDrugNum());
    }

    /**
     * 分页查询药品列表
     *
     * @param currentPage 当前页码
     * @param pageSize    每页数量
     * @return 查询结果
     */
    @Override
    public Result queryDrugListByPage(int currentPage, int pageSize) {
        return Result.ok(drugInfoMapper.queryDrugListByPage((currentPage - 1) * pageSize, pageSize));
    }

    /**
     * 添加药品
     *
     * @param drugInfo 药品信息
     * @return 添加结果
     */
    @Override
    public Result addDrug(DrugInfo drugInfo) {
        if (drugInfo.getDrugCode() == null || "".equals(drugInfo.getDrugCode())) {
            return Result.fail("添加失败,药品编码不能为空");
        }
        if (drugInfo.getDrugName() == null || "".equals(drugInfo.getDrugName())) {
            return Result.fail("药品名称不能为空");
        }
        drugInfo.setCreationDate(new Date());
        HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
        pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        String drugName = drugInfo.getDrugName();
        char[] drugNameCharArray = drugName.toCharArray();
        List<String> letterArray = new ArrayList<>(drugName.length());
        for (char c : drugNameCharArray) {
            try {
                String[] strings = PinyinHelper.toHanyuPinyinStringArray(c, pinyinOutputFormat);
                if (strings.length > 0) {
                    letterArray.add(String.valueOf(strings[0].charAt(0)));
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                throw new RuntimeException(e);
            }
        }
        StringBuffer target = new StringBuffer();
        letterArray.forEach(target::append);
        drugInfo.setMnemonicCode(target.toString());
        drugInfoMapper.addDrug(drugInfo);
        return Result.ok();
    }

    /**
     * 条件查询药品信息
     *
     * @param drugInfo 查询条件
     * @return 查询结果
     */
    @Override
    public Result queryDrugByCondition(DrugInfo drugInfo) {
        if (ObjectUtils.isEmpty(drugInfo)) {
            return Result.fail("查询条件不能为空");
        }
        return Result.ok(drugInfoMapper.queryDrugByCondition(drugInfo));
    }

    /**
     * 更新药品信息
     *
     * @param drugInfo 更新后的信息
     * @return 更新结果
     */
    @Override
    public Result updateDrugInfo(DrugInfo drugInfo) {
        if (drugInfo.getDrugCode() == null || "".equals(drugInfo.getDrugCode())) {
            return Result.fail("添加失败,药品编码不能为空");
        }
        if (drugInfo.getDrugName() == null || "".equals(drugInfo.getDrugName())) {
            return Result.fail("药品名称不能为空");
        }
        HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
        pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        String drugName = drugInfo.getDrugName();
        char[] drugNameCharArray = drugName.toCharArray();
        List<String> letterArray = new ArrayList<>(drugName.length());
        for (char c : drugNameCharArray) {
            try {
                String[] strings = PinyinHelper.toHanyuPinyinStringArray(c, pinyinOutputFormat);
                if (strings.length > 0) {
                    letterArray.add(String.valueOf(strings[0].charAt(0)));
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                throw new RuntimeException(e);
            }
        }
        StringBuffer target = new StringBuffer();
        letterArray.forEach(target::append);
        drugInfo.setMnemonicCode(target.toString());
        drugInfoMapper.updateDrugInfo(drugInfo);
        return Result.ok();
    }

    /**
     * 根据id删除药品
     *
     * @param id 药品id
     * @return 删除结果
     */
    @Override
    public Result deleteDrugById(int id) {
        drugInfoMapper.deleteDrugById(id);
        return Result.ok();
    }
}
